using System.Collections.Generic;
using Build.Interface;
using MoonFramework.Template;
using Science;
using Tool;
using UnityEngine;

namespace Map.MapObject.Building
{
    public abstract class BaseBuilding :BaseMapObject, IBuilding
    {
        [SerializeField] protected Collider _collider;
        private List<Material> _materials;
        [SerializeField] private int scienceID;

        public override void Init(BaseMapBlock mapBlock, ulong id)
        {
            base.Init(mapBlock, id);
            ScienceManager.Instance.AddScience(scienceID);
        }

        #region 预览模式
        public GameObject BuildingObj => gameObject;
        public Collider Collider => _collider;
        public List<Material> Materials { get => _materials; set => _materials = value; }

        public virtual void OnPreview() { }

        /// <summary>
        /// 通用碰撞检测方法
        /// </summary>
        public abstract bool CollisionDetectionOnPreview(LayerMask layerMask);

        /// <summary>
        /// 盒形碰撞体检测方法
        /// </summary>
        public bool BoxCollisionDetectionOnPreview(LayerMask layerMask)
        {
            BoxCollider boxCollider = (BoxCollider)Collider;
            return Physics.CheckBox(boxCollider.transform.position + boxCollider.center, boxCollider.size/2, boxCollider.transform.rotation, layerMask);
        }

        /// <summary>
        /// 球形碰撞体检测方法
        /// </summary>
        public bool SphereCollisionDetectionOnPreview(LayerMask layerMask)
        {
            SphereCollider sphereCollider = (SphereCollider)Collider;
            return Physics.CheckSphere(sphereCollider.transform.position + sphereCollider.center, sphereCollider.radius, layerMask);
        }

        /// <summary>
        /// 胶囊体形碰撞检测方法
        /// </summary>
        public bool CapsuleCollisionDetectionOnPreview(LayerMask layerMask)
        {
            CapsuleCollider capsuleCollider = (CapsuleCollider)Collider;
            Vector3 colliderCenter = capsuleCollider.transform.position + capsuleCollider.center;
            Vector3 startPos = colliderCenter;
            Vector3 endPos = colliderCenter;
            startPos.y += capsuleCollider.radius - capsuleCollider.height/2;
            endPos.y -= capsuleCollider.radius - capsuleCollider.height/2;
            return Physics.CheckCapsule(startPos, endPos, capsuleCollider.radius, layerMask);
        }
        #endregion

        #region 运行时

        /// <summary>
        /// 当建筑被鼠标选中时
        /// </summary>
        public virtual void OnSelect() { }

        /// <summary>
        /// 当前物品格子结束拖拽时选中
        /// </summary>
        public virtual bool OnSlotEndDragSelect(int itemID) => false;
        #endregion
    }
}